<?php

namespace app\common\dao\contract;

use think\db\BaseQuery;
use app\common\dao\BaseDao;
use app\common\model\contract\ContractBillModel;

class ContractBillDao extends BaseDao
{

    /**
     * @return ContractBillModel
     */
    protected function getModel(): string
    {
        return ContractBillModel::class;
    }

    /**
     * @param array $where
     * @return BaseQuery
     */
    public function search(array $where, int $propertyId = null)
    {
        return $this->getModel()::getDB()
            ->when($propertyId !== null, function ($query) use ($propertyId) {
                $query->where('property_id', $propertyId);
            })
            ->when(isset($where['contract_id']) && $where['contract_id'] !== '', function ($query) use ($where) {
                $query->where('contract_id', (int)$where['contract_id']);
            })
            ->when(isset($where['room_id']) && $where['room_id'] !== '', function ($query) use ($where) {
                $query->where('room_id', (int)$where['room_id']);
            })
            ->when(isset($where['time']) && $where['time'] !== '', function ($query) use ($where) {
                $this->timeSearchBuild($query, $where['add_time'], 'add_time');
            });
    }

}
